package com.rs.game.world.area;

import java.util.ArrayList;
import java.util.List;

import com.rs.game.WorldTile;
import com.rs.game.player.Player;
import com.rs.game.tasks.WorldTask;
import com.rs.game.tasks.WorldTasksManager;
import com.rs.game.world.area.impl.Alkharid;
import com.rs.game.world.area.impl.Falador;
import com.rs.game.world.area.impl.Lumbridge;
import com.rs.game.world.area.impl.Varrock;
import com.rs.game.world.area.shapes.Shape;
import com.rs.utils.Logger;
import com.rs.utils.Utils;

public class AreaManager {

	private static List<Area> areas;

	public static void init() {
		try {
			areas(new ArrayList<Area>());
			areas().add(new Varrock());
			areas().add(new Falador());
			areas().add(new Lumbridge());
			areas().add(new Alkharid());
		} catch (Exception e) {
			Logger.handle(e);
		}
	}

	public static Area get(final WorldTile location) {
		try {
			Area current = null;
			boolean found = false;
			for (Area area : areas()) {

				for (Shape shape : area.shapes()) {

					if (shape.inside(location)) {
						current = area;
						found = true;
						break;
					}

				}

				if (!found)
					continue;

				return current;
			}
		} catch (Exception e) {
			Logger.handle(e);
		}
		return null;
	}

	public static void update(final Player player, final Area area) {
		if (player.getFreezeDelay() > Utils.currentTimeMillis())
			return;
		player.getPackets().sendIComponentText(1073, 10, "<col=ffffff>You have reached");
		player.getPackets().sendIComponentText(1073, 11, "<col=ffcff00>" + area.name());
		player.getInterfaceManager().setWindowInterface(player.getInterfaceManager().hasRezizableScreen() ? 1 : 11, 1073);
		WorldTasksManager.schedule(new WorldTask() {
			@Override
			public void run() {
				player.getPackets().closeInterface(player.getInterfaceManager().hasRezizableScreen() ? 1 : 11);
				stop();
			}
		});
	}

	public static List<Area> areas() {
		return areas;
	}

	public static void areas(List<Area> list) {
		areas = list;
	}

}